<template>
    
                <div class="v-list" v-loading="loading" element-loading-text="加载中">

        <el-card class="box-card">
            <div slot="header" class="clearfix">
                <span class="title">
                管理员列表
                </span>

            </div>
                <!-- 搜索 -->
        <div class="form-search">
            <el-form @submit.prevent.stop :inline="true" size="mini">
                                <el-form-item label="帐号">
                    <!-- 输入的名字 -->
                    <el-input v-model="search.username"></el-input>
                    
                </el-form-item>                <el-form-item>
                    <el-button type="primary" @click="searchSubmit" icon="el-icon-search">查询</el-button>
                </el-form-item>
                            </el-form>
        </div>
        

        <el-table border :data="list" style="width: 100%" highlight-current-row
                    >
            
            <el-table-column type="index" label="序号" width="90"></el-table-column> <!-- 序号 -->

                        <el-table-column label="帐号" width="130">
                <template slot-scope="{row}">
                    {{  row.username  }}                </template>
            </el-table-column>
                        <el-table-column label="密码">
                <template slot-scope="{row}">
                    {{  row.pwd  }}                </template>
            </el-table-column>
            


                        
            <el-table-column label="操作">
                <template slot-scope="{row}">
                    <el-button-group>
                    
                                            <el-tooltip content="编辑" placement="top">
                    <el-button icon="el-icon-edit" @click="$goto({path:'/admin/adminsupdt',query:{id:row.id } })"
                               type="warning" size="mini"></el-button>
                        </el-tooltip>
                        <el-tooltip content="删除" placement="top">
                    <el-button icon="el-icon-delete" type="danger" size="mini" @click="deleteItem(row)">

                    </el-button>
                        </el-tooltip>
                    </el-button-group>
                </template>
            </el-table-column>
        </el-table>


            
        <div class="e-pages" style="margin-top: 10px;text-align: center">
            <el-pagination
                    @current-change="loadList"
                    :current-page="page"
                    :page-size="pagesize"
                    @size-change="sizeChange"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="totalCount">
            </el-pagination>
        </div>

                </el-card>
    </div>
</template>
<style type="text/scss" scoped lang="scss">

</style>
<script>
    import api from '@/api';
    import { remove , checkIssh } from '@/utils/utils';
    import { extend } from '@/utils/extend';
    import objectDiff from 'objectdiff';

    
    /**
     * 后台列表页面
     */
    export default {
        data() {
            return {

                loading:false,
                list:[],
                search:{
                username:'',
                },
                total:{},
                page:1, // 当前页
                pagesize:10, // 页大小
                totalCount:0, // 总行数
            
            
            }
        },
        watch: {},
        computed: {},
        methods: {
            searchSubmit(){
                this.loadList(1);
            },
            
                sizeChange(e){
                this.pagesize = e;
                this.loadList(1);
            },
            checkIssh,
            
            //查询全部列表
            loadList( page ){
                // 防止重新点加载列表
                if(this.loading)return;
                this.page = page;
                // 筛选条件，有筛选条件便进行条件选择
                var filter = extend(true, {}, this.search, {page:page+"", pagesize: this.pagesize+""});
                var diff = objectDiff.diff(filter, this.$route.query);
                if (diff.changed != 'equal') { // 筛选的条件不一致则更新链接
                    this.$router.push({  // 更新query
                        path: this.$route.path,
                        query: filter
                    });
                }
                //提交输入框filter值，进行条件分页模糊查询，刚加载时候是无数据的，便是查询全部
                this.$post(api.admins.list , filter).then(res=>{
                    if(res.code == api.code.OK)
                    {
                        extend(this , res.data);
                    }else{
                        this.$message.error(res.msg);
                    }
                }).catch(err=>{
                    this.loading = false;
                    this.$message.error(err.message);
                });
            },

            // 删除方法
            deleteItem( row ){
                //弹出框进行提示,提示是否要删除
                this.$confirm("确定删除数据？","提示",{
                    type: 'warining'
                }).then(() => {
                    //提交到后台,确定操作
                    this.$post(api.admins.delete,{
                        id: row.id
                    }).then(res => {
                        if(res.code == api.code.OK){
                            remove(this.list, row)//确定操作
                        }else{
                            this.$message.error(res.msg)//操作错误
                        }
                    })
                }).then(() => {
                    // 取消操作
                });

            }
            
        },

        beforeRouteUpdate(to,form,next){
            extend(this.search , to.query)
            this.loadList(1);
            next();
        },
        created() {
            var search = extend(this.search , this.$route.query)
            if(search.page)
            {
                this.page = Math.floor(this.$route.query.page)
                delete search.page
            }
            if(search.pagesize)
            {
                this.pagesize = Math.floor(this.$route.query.pagesize)
                delete search.pagesize
            }

            this.loadList(1);
                    },
        mounted() {
                    },
        destroyed() {
                    }
    }
</script>
